package com.hf.eagle.web.struts.admin.manager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.DynaBean;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ebiz.ssi.util.EncryptUtils;
import com.hf.eagle.domain.UserInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Keys;

/**
 * @author Xu,XiaoYuan
 * @version 2012.3.25 13:31
 */
public class UserPasswdAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.edit(mapping, form, request, response);
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");

		HttpSession session = request.getSession();
		UserInfo ui = (UserInfo) session.getAttribute(Keys.ADMIN_USER);

		UserInfo userInfo = new UserInfo();
		userInfo.setUser_id(ui.getUser_id());
		userInfo.setIs_del(0);
		userInfo = super.getFacade().getUserInfoService().getUserInfo(userInfo);
		super.copyProperties(form, userInfo);

		dynaBean.set("mod_id", mod_id);

		return mapping.findForward("input");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		UserInfo entity = new UserInfo();
		super.copyProperties(entity, form);

		String password = entity.getPassword();

		UserInfo userInfo = getFacade().getUserInfoService().getUserInfo(new UserInfo(entity.getUser_id()));
		if (null != userInfo) {
			if (!password.equalsIgnoreCase(userInfo.getPassword())) {
				entity.setPassword(EncryptUtils.MD5Encode(password));
			}
			super.getFacade().getUserInfoService().modifyUserInfo(entity);
			saveMessage(request, "entity.updated");
		} else {
			saveError(request, "entity.missing");
		}

		return this.edit(mapping, form, request, response);
	}

}
